/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_stdtypes.h"

ZlangInvokeFunction ZlangInvokeFunction_Compile_string;
int ZlangInvokeFunction_Compile_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_regexp	*obj_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char					*pattern = NULL ;
	int32_t					pattern_len ;
	const char				*errptr = NULL ;
	int					erroffset ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in1 , & pattern , & pattern_len );
	
	if( obj_direct_prop->pattern_re )
	{
		pcre_free(obj_direct_prop->pattern_re) ;
	}

	obj_direct_prop->pattern_re = pcre_compile( pattern , 0 , & errptr , & erroffset , NULL ) ;
	if( obj_direct_prop->pattern_re == NULL )
	{
		TEST_RUNTIME_DEBUG_THEN_PRINT( rt , "pcre_compile[%s] failed" , pattern )
		CallRuntimeFunction_bool_SetBoolValue( rt , out1 , FALSE );
		return 0;
	}
	else
	{
		TEST_RUNTIME_DEBUG_THEN_PRINT( rt , "pcre_compile[%s] ok" , pattern )
		CallRuntimeFunction_bool_SetBoolValue( rt , out1 , TRUE );
	}
	
	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_regexp;
void *ZlangCreateDirectProperty_regexp( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_regexp	*obj_direct_prop = NULL ;
	
	obj_direct_prop = (struct ZlangDirectProperty_regexp *)ZLMALLOC( sizeof(struct ZlangDirectProperty_regexp) ) ;
	if( obj_direct_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( obj_direct_prop , 0x00 , sizeof(struct ZlangDirectProperty_regexp) );
	
	return (void *)obj_direct_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_regexp;
void ZlangDestroyDirectProperty_regexp( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_regexp	*obj_direct_prop = GetObjectDirectProperty(obj) ;
	
	if( obj_direct_prop->pattern_re )
	{
		pcre_free(obj_direct_prop->pattern_re) ;
	}

	ZLFREE( obj_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_regexp;
void ZlangSummarizeDirectPropertySize_regexp( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	struct ZlangDirectProperty_regexp	*direct_prop = GetObjectDirectProperty(obj) ;
	
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_regexp)+sizeof(direct_prop->pattern_re) )
	
	return;
}

static struct ZlangDirectFunctions direct_funcs_regexp =
	{
		ZLANG_OBJECT_regexp ,
		
		ZlangCreateDirectProperty_regexp ,
		ZlangDestroyDirectProperty_regexp ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		ZlangSummarizeDirectPropertySize_regexp ,
	} ;

ZlangImportObjectFunction ZlangImportObject_regexp;
struct ZlangObject *ZlangImportObject_regexp( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_regexp , & direct_funcs_regexp , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_Compile_string , ZLANG_OBJECT_bool , "Compile" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	return obj ;
}
